import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/main/main.vue'
import { localCache } from '@/utils/cache'
import { LOGIN_TOKEN } from '@/common/constant'
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../views/main/main.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/login.vue')
    }
  ]
})

// 导航守卫
router.beforeEach((to) => {
  // 只有登录成功(token), 才能真正进入到main页面
  const token = localCache.getCache(LOGIN_TOKEN)
  if (to.path.startsWith('/main') && !token) {
    return '/login'
  }
  // TODO 跳转到主页逻辑
  // 如果是进入到main
  // if (to.path === '/main') {
  //   return firstMenu?.url
  // }
})

export default router
